// pages/personal/index.js
import { memberInformation, editInfo } from '../../api/index'
import { checkUrl } from "../../utils/common"
import { baseUrl } from "../../utils/config"
import QRCode from '../../utils/webapp-qrcode'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    url: '',
    ScreenBrightness: '',
    name: ''
  },
  yulan() {
    wx.previewImage({
      urls: [this.data.url],
    })
  },
  xyTJ() {
    wx.removeStorageSync('userId');
    wx.removeStorageSync('nickName');
    wx.reLaunch({
      url: '/pages/login/index'
    })
  },
  previewImages: function() {
    let url = 'common/oss/uploadOss.do'
    let that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: (fh) => {
        const tempFilePaths = fh.tempFilePaths
        for (let i = 0; i < tempFilePaths.length; i += 1) {
          wx.uploadFile({
            url: checkUrl(url) ? url : baseUrl + url,
            filePath: tempFilePaths[i],
            name: 'file',
            header: {
              'content-type': 'application/json', // 默认值
              Cookie: wx.getStorageSync('cookie'),
              gymId: wx.getStorageSync('GYMID')
            },
            formData: {
              folder: 'USER_IMG', 
              type: '' 
            },
            success(res) {
              let userInfo = that.data.userInfo
              userInfo.face = JSON.parse(res.data).insertUrl
              userInfo.faceUrl = JSON.parse(res.data).showUrl
              that.setData({ userInfo});
              that.editInfo()
            },
          });
        }
      }
    })
  },
  editInfo() {
    if(!this.data.userInfo.faceUrl) {
      return false
    }
    editInfo({
      param: {
        userId: wx.getStorageSync('userId'),
        ...this.data.userInfo
      }
    }).then(res => {
      if (res.errorCode != '000000') return
      this.memberInformation()
    })
  },
  // 获取会员信息
  memberInformation() {
    memberInformation({
      param: {
        userId: wx.getStorageSync('userId')
      }
    }).then(res => {
      if (res.errorCode != '000000') return
      let that = this
      new QRCode('canvas', {
        text: res.record.code,
        width: 150,
        height: 150,
        padding: 12,
        correctLevel: QRCode.CorrectLevel.L,
        callback: (ss) => {
          console.log(3, ss)
          that.setData({ url: ss.path })
        }
      })
      this.setData({
        userInfo: res.record,
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.memberInformation()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    wx.setScreenBrightness({
      value: this.data.ScreenBrightness
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})